<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    width="60%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-tabs v-model="tabkey">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文" :forceRender="true"></a-tab-pane>
    </a-tabs>
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules" 
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <!-- <a-form-model-item label="检测案例图片" required>
        <a-upload
          list-type="picture-card"
          :file-list="fileList"
          :remove="hanldeImgRemove"
          @preview="handlePreview"
          :customRequest="handleUploadChange"
          class="upload-half"
          accept="image/*"
        >
          <div class="custom-btn" v-if="fileList.length<1">
            <a-icon type="ant-cloud" style="font-size: 26px" />
            <div class="ant-upload-text">添加图片</div>
          </div>
        </a-upload>
      </a-form-model-item> -->
      <a-form-model-item label="标题" prop="title" v-if="tabkey=='1'">
        <a-input v-model="form.title" placeholder="标题"></a-input>
      </a-form-model-item>
      <a-form-model-item label="英文标题" prop="title" v-else>
        <a-input v-model="form.titleEn" placeholder="英文标题"></a-input>
      </a-form-model-item>
      <a-form-model-item label="检测案例" required v-if="tabkey=='1'">
        <wangEditor :value="form.description" @change="editorChange" id="content1" :key="tabkey"></wangEditor>
      </a-form-model-item>
      <a-form-model-item label="英文新闻内容" required v-else>
        <wangEditor :value="form.descriptionEn" @change="editorChange1" id="content2" :key="tabkey"></wangEditor>
      </a-form-model-item>
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
    <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible = false" width="60%">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </a-drawer>
</template>

<script>
import { updateTestCase } from '@/api/manage'
import { showMessage } from '@/utils/mixins'
import { uploadFile,deleteFile } from '@/api/common'
import { getFileName } from '@/utils/util'
import wangEditor from '@/components/Editor/WangEditor.vue'

export default {
  mixins: [showMessage],
  components: {
    wangEditor
  },
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      tabkey: '1',
      visible: false,
      title: '',
      labelCol: {span: 3},
      wrapperCol: {span: 21},
      form: {
        type: undefined,
        description: undefined,
        descriptionEn: undefined,
        title: undefined,
        titleEn: undefined
      },
      rules: {
        description: [{required: true, message: '案例内容不能为空', trigger: 'blur'}],
        descriptionEn: [{required: true, message: '英文案例内容不能为空', trigger: 'blur'}],
        title: [{required: true, message: '标题不能为空', trigger: 'blur'}],
        titleEn: [{required: true, message: '英文标题不能为空', trigger: 'blur'}],
      },
      id: undefined,
      fileList: [],
      previewVisible: false,
      previewImage: ''
    }
  },
  methods: {
    // 预览图片
    handlePreview(file) {
      this.previewVisible = true
      this.previewImage = file.url
    },
    // 删除图片
    hanldeImgRemove(file) {
      deleteFile({filePath: file.url}).then(res => {
        if(res.code == '0') {
          this.showMessage(res,() => {
            this.fileList = []
          })
        }
      })
    },
    //自定义上传图片
    handleUploadChange(file) {
      const _this = this
      file = file.file
      const formdata = new FormData()
      formdata.append('file', file)
      uploadFile(formdata).then(res => {
        if(res.code == '0') {
          file.url = process.env.VUE_APP_API_ORIGIN+res.data
          file.uid = Math.random()
          file.fileame = getFileName( res.data )
          _this.fileList.push(file)
          _this.form.img = res.data
        }else {
          _this.$message.error(res.message)
        }
      })
    },
    editorChange(val) {
      this.form.description = val
    },
    editorChange1(val) {
      this.form.descriptionEn = val
    },
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          if(!this.form.description) {
            this.$message.warning('案例内容不能为空！')
            return
          }
          updateTestCase({
            id: this.id,
            ...this.form
          }).then(res => {
            this.showMessage(res, () => {
              this.onClose()
              this.parent.getList()
            })
          })
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      for(const key in this.form) {
        this.form[key] = undefined
      }
      this.visible = false
      this.id = undefined
      this.fileList = []
      this.tabkey = '1'
    }
  }
}
</script>